package com.tgz.controller;


import com.tgz.common.R;
import com.tgz.config.MyVar;
import com.tgz.websocket.Message;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.messaging.handler.annotation.MessageMapping;
import org.springframework.messaging.handler.annotation.Payload;
import org.springframework.messaging.simp.SimpMessagingTemplate;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.security.Principal;

/**
 * <p>
 * 弹幕 前端控制器
 * </p>
 *
 * @author hsj
 * @since 2022-12-22
 */
@Slf4j
@RestController
public class BscreenOneToOneController {
    @Autowired
    public SimpMessagingTemplate template;
    @Autowired
    private MyVar myVar;
    /**
     * 点对点
     */
    @MessageMapping("/websocketClientDirectorPushToOne")//stompClient直接发送消息，不走http中转
    public Object pushToOne(@RequestBody Message msg) {

        /**
            使用convertAndSendToUser方法，第一个参数为用户id，此时js中的订阅地址为：
            "/user/" + 用户Id + "/message",其中"/user"是固定的
         */
        template.convertAndSendToUser(msg.getTo(), myVar.getTopicUserChannel(), msg);
        return R.responseWithCodeStatusMessageDataEnhance();
    }

}
